extends Projectile


class_name BaseBullet


@onready var hit_box: HitBoxComponent = $HitBoxComponent
@onready var move: MoveComponent = $MoveComponent
@onready var timer: Timer = $Timer


func _ready() -> void:
    super._ready()
    hit_box.event_hit_something.connect(_on_hit_something)

func __set_enable(value: bool) -> void:
    super.__set_enable(value)
    hit_box.is_enable = value
    move.is_enable = value
    visible = value

func setup(arg: ProjectileResource) -> void:
    position = arg.start_position
    move.direction = arg.start_direction
    move.speed = arg.speed
    timer.wait_time = arg.limit_lifetime

    timer.start()

func clone_source() -> ProjectileResource:
    var arg: ProjectileResource = ProjectileResource.new()
    arg.start_position = position
    arg.start_direction = move.direction
    arg.speed = move.speed
    arg.limit_lifetime = timer.time_left
    return arg

func _on_hit_something(hitbox: HitBoxComponent, body: Node2D, body_rid: RID, hit_normal: Vector2) -> void:
    event_hit_something.emit(self, body, body_rid, hit_normal)

func _on_timer_timeout() -> void:
    event_hit_nothing.emit(self)

func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
    event_hit_out_of_screen.emit(self, move.direction)
